var t = require("../../@babel/runtime/helpers/interopRequireDefault")(require("../../@babel/runtime/regenerator")), e = require("../../@babel/runtime/helpers/asyncToGenerator"), a = getApp();

Page({
    data: {
        prompt_id: 0,
        prompt: {},
        lists: [],
        message: "",
        balance: 0,
        vip_expire_time: "",
        scrollTop: 0,
        shareTitle: "",
        shareImage: "",
        timeline_share_id: 0,
        lang: "简体中文",
        langs: [ "简体中文", "繁体中文", "English" ],
        inputShow: !0
    },
    onLoad: function(t) {
        var e = this;
        t.sid && wx.setStorageSync("sid", t.sid), t.prompt_id && this.setData({
            prompt_id: t.prompt_id
        }), a.util.checkLogin().then(function() {
            e.getPrompt(), e.getWxappInfo(), e.getHistoryMsg();
        });
    },
    onChangeText(e) {
      this.setData({
        message: e.detail.value
      })
    },
    changeLang: function(t) {
        var e = t.currentTarget.dataset.lang;
        this.setData({
            lang: e
        }), a.util.message("已切换为【" + e + "】输出");
    },
    getHistoryMsg: function() {
        var t = this;
        a.util.request({
            url: "/write/getHistoryMsg",
            data: {
                prompt_id: this.data.prompt_id
            }
        }).then(function(e) {
            e.data.length > 0 && t.setData({
                lists: e.data
            }), t.scrollToBottom();
        });
    },
    getPrompt: function() {
        var t = this;
        a.util.request({
            url: "/write/getPrompt",
            data: {
                prompt_id: this.data.prompt_id
            }
        }).then(function(e) {
            t.setData({
                prompt: e.data
            }), wx.setNavigationBarTitle({
                title: e.data.title
            });
        });
    },
    sendText: function() {
        var t = this, e = this.data.message;
        e ? a.util.request({
            url: "/wxapp/wordFilter",
            data: {
                message: e
            },
            loading: !0
        }).then(function(i) {
            var r = i.data, s = t.data.lists;
            s.push({
                user: "我",
                message: r.split("\n")
            }), s.push({
                user: "ChatGPT",
                message: [ "正在输入.." ]
            }), t.setData({
                message: "",
                lists: s
            }), t.scrollToBottom(), a.util.request({
                url: "/write/sendText",
                data: {
                    message: e,
                    prompt_id: t.data.prompt_id,
                    lang: t.data.lang
                },
                loading: !1,
                timeout: 12e4
            }).then(function(e) {
                (s = s.slice(0, -1)).push({
                    user: "ChatGPT",
                    message: e.data
                }), t.setData({
                    lists: s
                }), t.scrollToBottom();
            }).catch(function(e) {
                (s = s.slice(0, -1)).push({
                    user: "ChatGPT",
                    message: [ "网络错误，本条消息不扣费。" ]
                }), t.setData({
                    lists: s
                });
            });
        }) : console.log("no input");
    },
    sendConfirm: function() {
        var t = this;
        setTimeout(function() {
            t.sendText();
        }, 50);
    },
    scrollToBottom: function() {
        var t = this;
        setTimeout(function() {
            var e = wx.createSelectorQuery().in(t);
            e.select(".list").boundingClientRect(function(e) {
                t.setData({
                    scrollTop: e.height
                });
            }), e.exec(function(t) {});
        }, 100);
    },
    copyText: function(t) {
        var e = t.currentTarget.dataset.index, a = this.data.lists[e].message;
        a = (a = a.join("\n")).replaceAll("&nbsp;", " "), wx.setClipboardData({
            data: a
        });
    },
    getWxappInfo: function() {
        var t = this;
        return new Promise(function(e, i) {
            a.util.request({
                url: "/wxapp/getWxappInfo",
                loading: !1
            }).then(function(a) {
                t.setData({
                    shareTitle: a.data.share_title,
                    shareImage: a.data.share_image,
                    is_check: a.data.is_check
                }), e();
            });
        });
    },
    showInput: function() {
        this.setData({
            inputShow: !this.data.inputShow
        });
    },
    onShareAppMessage: function() {
        var a = this;
        return e(t.default.mark(function e() {
            var i;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return t.next = 2, a.getShareId("wechat");

                  case 2:
                    return i = t.sent, console.log("sh", i), t.abrupt("return", {
                        title: a.data.shareTitle,
                        imageUrl: a.data.shareImage,
                        path: "/pages/write/chat?prompt_id=" + a.data.prompt_id + "&sid=" + i
                    });

                  case 5:
                  case "end":
                    return t.stop();
                }
            }, e);
        }))();
    },
    onShareTimeline: function() {
        var t = this.data.timeline_share_id;
        return t || this.getShareId("timeline"), console.log("share_id", t), {
            title: this.data.shareTitle,
            imageUrl: this.data.shareImage,
            query: "prompt_id=" + this.data.prompt_id + "&sid=" + t
        };
    },
    getShareId: function(i) {
        var r = this;
        return e(t.default.mark(function e() {
            var s;
            return t.default.wrap(function(t) {
                for (;;) switch (t.prev = t.next) {
                  case 0:
                    return s = 0, t.next = 3, a.util.request({
                        url: "/wxapp/doShare",
                        data: {
                            way: i
                        }
                    }).then(function(t) {
                        s = t.data.share_id, "timeline" == i && r.setData({
                            timeline_share_id: s
                        });
                    });

                  case 3:
                    return t.abrupt("return", s);

                  case 4:
                  case "end":
                    return t.stop();
                }
            }, e);
        }))();
    },
    onAddToFavorites: function() {
        return {
            title: this.data.shareTitle,
            imageUrl: this.data.shareImage
        };
    }
});